<template>
	<van-address-list :list="list" @add="onAdd" @edit="onEdit" :switchable="false" default-tag-text="默认" />
</template>

<script setup>
import { getCurrentInstance } from 'vue';

const { proxy } = getCurrentInstance();

// 会员数据
const business = proxy.$cookies.get('business');

const list = ref([]);

// const chosenAddressId = ref('1');

// 请求方法
async function getData() {
	const res = await proxy.$POST({
		url: '/address/index',
		params: { busid: business.id },
	});

	if (res.code === 0) {
		proxy.$message(res.msg);
		return;
	}

	list.value = res.data.map((t) => {
		// if (t.status === '1') {
		// 	// 设置选择的地址
		// 	chosenAddressId.value = t.id;
		// }
		return {
			...t,
			// 是否默认地址
			isDefault: t.status === '1',
			name: t.consignee,
			tel: t.mobile,
			address: t.region_text + t.address,
		};
	});
}

function onAdd() {
	proxy.$router.push('/address/add');
}
function onEdit(s) {
	proxy.$router.push({
		path: '/address/edit',
		query: { id: s.id },
	});
}

getData();
</script>

<style scoped></style>
